package com.avic.rong.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.activity.mine.MyFansActivity;
import com.avic.rong.api.focus_fans.FocusStateApi;
import com.avic.rong.bean.focus_fans.FansBean;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyFansAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private ArrayList<FansBean.ListBean> mList;

    private String token;



    public MyFansAdapter(Context mContext, ArrayList<FansBean.ListBean> mList,String token) {
        this.mContext = mContext;
        this.mList = mList;
        this.token = token;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_fans, viewGroup, false);
        return new FansHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        FansBean.ListBean fansBean = mList.get(i);
        final FansHolder fansHolder = (FansHolder) viewHolder;
        fansHolder.tv_name.setText(fansBean.getUser_name());
        fansHolder.tv_artical.setText("文章"+fansBean.getArticle_num());
        fansHolder.tv_fens.setText("粉丝"+fansBean.getFollower_num());
        Glide.with(mContext).applyDefaultRequestOptions(RequestOptions.circleCropTransform().error(R.mipmap.deafult_image_header))
                .load(fansBean.getLogo()).into(fansHolder.image_header);




        boolean is_foller = fansBean.isIs_foller();
        if (is_foller){
            fansHolder.bt_focuson.setBackgroundResource(R.mipmap.fans_attentioned);
            /*fansHolder.bt_focuson.setTextColor(R.color.color_title);
            fansHolder.bt_focuson.setBackgroundResource(R.drawable.fans_shape);*/
        }else {
            fansHolder.bt_focuson.setBackgroundResource(R.mipmap.fans_attention);
        }

        int isV = fansBean.getIsV();
        if (isV == 1){
            fansHolder.ivVip.setVisibility(View.VISIBLE);
        }else {
            fansHolder.ivVip.setVisibility(View.GONE);
        }


        fansHolder.bt_focuson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtil.isLogin()){
                    FocusStateApi focusStateApi = new FocusStateApi((MyFansActivity)mContext, new FocusStateListener(fansBean,fansHolder));
                    focusStateApi.setArgument(fansBean.getUser_id(),SharedPreferencesUtil.getInstance(mContext).getLogintoken());
                    HttpManager.getInstance().doHttpDeal(focusStateApi);
                }else{
                    mContext.startActivity(LoginPhoneActivity.getIntent(mContext,"MyFansActivity"));
                }
            }
        });


        fansHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (DateUtils.isFastDoubleClick()) return;

                Intent PresenIntent = new Intent(mContext, PersonalHomeActivity.class);
                PresenIntent.putExtra("homeUserID", fansBean.getUser_id() + "");
                mContext.startActivity(PresenIntent);
            }
        });

    }

    public class  FocusStateListener extends  HttpOnNextListener<BaseResultEntity>{

        FansBean.ListBean listBean;
        FansHolder fansHolder;

        public FocusStateListener(FansBean.ListBean listBean, FansHolder fansHolder) {
            this.listBean = listBean;
            this.fansHolder = fansHolder;
        }
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)){
                if (baseResultEntity.getMsg().equals("取消关注成功")){
                    fansHolder.bt_focuson.setBackgroundResource(R.mipmap.fans_attention);
                    listBean.setIs_foller(false);
                }else {
                    fansHolder.bt_focuson.setBackgroundResource(R.mipmap.fans_attentioned);
                    listBean.setIs_foller(true);
                }
                ToastTools.showPublicToast(baseResultEntity.getMsg());
            }else {
                fansHolder.bt_focuson.setChecked(listBean.isIs_foller());
                ToastTools.showPublicToast(baseResultEntity.getMsg());
            }
        }
    };


    @Override
    public int getItemCount() {
        return mList.size();
    }
    class FansHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_header)
        ImageView image_header;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_fens)
        TextView tv_fens;
        @BindView(R.id.tv_artical)
        TextView tv_artical;
        @BindView(R.id.bt_focuson)
        CheckBox bt_focuson;
        @BindView(R.id.iv_vip)
        ImageView ivVip;
        public FansHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }


}
